package com.cdvtc.forum_system.web.servlet;
import com.cdvtc.forum_system.model.User;
import com.cdvtc.forum_system.service.UserService;
import com.cdvtc.forum_system.service.Impl.UserServiceImpl;
import com.cdvtc.forum_system.util.Md5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求体编码，防止中文乱码
        request.setCharacterEncoding("UTF-8");
        String account = request.getParameter("account");
        String nickname = request.getParameter("nickname");
        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");


        // 1. 基本校验：非空、两次密码一致
        if (account == null || account.trim().isEmpty()
                || nickname == null || nickname.trim().isEmpty()
                || password == null || password.trim().isEmpty()
                || confirmPassword == null || confirmPassword.trim().isEmpty()
                ||!password.equals(confirmPassword)) {
            request.setAttribute("error", "输入信息有误，请检查！（账号、昵称、密码不能为空，且两次密码需一致）");
            request.getRequestDispatcher("index.jsp").forward(request, response);
            return;
        }

        // 2. 调用 Service 层，检查账号是否已存在
        boolean accountExisted = userService.isUserExisted("account", account);
        if (accountExisted) {
            request.setAttribute("error", "该账号已存在，请更换账号！");
            request.getRequestDispatcher("index.jsp").forward(request, response);
            return;
        }

        // 3. 检查昵称是否已存在
        boolean nicknameExisted = userService.isUserExisted("nickname", nickname);
        if (nicknameExisted) {
            request.setAttribute("error", "该昵称已被使用，请更换昵称！");
            request.getRequestDispatcher("index.jsp").forward(request, response);
            return;
        }

        // 4. 密码 MD5 加密（如果需要的话，结合你项目里的加密逻辑，比如 Md5Util ）
        String md5Password = Md5Util.md5(password);

        // 5. 构建 User 对象，根据实际需求补充其他字段（如生日、邮箱等，这里示例只填必要的 ）


// ...其他必要的包引入...

// 构建 User 对象的完整代码
        User user = new User();
        user.setUser_account(account);
        user.setUser_nickname(nickname);
        user.setUser_password(md5Password);
        user.setUser_photo("default_photo.jpg"); // 默认头像
        user.setUser_birthday(new Date()); // 假设默认生日为当前日期

// 从表单获取其他可选字段（如果有），没有则使用默认值
        String email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        user.setUser_email(email != null ? email : ""); // 邮箱为空则设空字符串
        user.setUser_mobile(mobile != null ? mobile : ""); // 手机号为空则设空字符串

// 补充注册时间（使用当前时间）
        user.setUser_regdate(new Date()); // 使用 java.util.Date 类型

// 补充是否禁用（新注册用户默认不禁用）
        user.setForbidden(false); // Boolean 类型，false 表示不禁用

        // 6. 调用 Service 层添加用户
        boolean addResult = userService.addUser(user);
        if (addResult) {
            // 注册成功，可跳转到登录页并提示
            request.setAttribute("success", "注册成功，请前往登录！");
            request.getRequestDispatcher("forum_login.jsp").forward(request, response);
        } else {
            // 注册失败，返回注册页提示
            request.setAttribute("error", "注册失败，请稍后重试！");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }
}
